package com.xhtt.modules.photo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.exception.RRExceptionEnum;
import com.xhtt.common.utils.DateUtils;
import com.xhtt.common.utils.ExportExcel;
import com.xhtt.common.utils.PageUtils;
import com.xhtt.common.utils.Query;
import com.xhtt.modules.cfg.entity.CfgWorkArea;
import com.xhtt.modules.cfg.service.CfgWorkAreaService;
import com.xhtt.modules.photo.dao.PhotoProblemMapper;
import com.xhtt.modules.photo.entity.PhotoProblem;
import com.xhtt.modules.photo.service.IPhotoProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 现场拍照问题记录 服务实现类
 * </p>
 *
 * @author xj
 * @since 2019-02-14
 */
@Service
public class PhotoProblemServiceImpl extends ServiceImpl<PhotoProblemMapper, PhotoProblem> implements IPhotoProblemService {

    @Autowired
    private CfgWorkAreaService cfgWorkAreaService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        Page<PhotoProblem> page = new Query<PhotoProblem>(params).getPage();

        LocalDateTime startDate = Optional.ofNullable(params.get("startDate")).isPresent() ? DateUtils.string2LocalDateTime((String) params.get("startDate"), "yyyy-MM-dd 00:00:00") : null;

        LocalDateTime endDate = Optional.ofNullable(params.get("endDate")).isPresent() ? DateUtils.string2LocalDateTime((String) params.get("endDate"), "yyyy-MM-dd 00:00:00").plusDays(1) : null;

        // app端一天的数据
        LocalDateTime onceStart = null;
        LocalDateTime onceEnd = null;
        if (Optional.ofNullable(params.get("datetime")).isPresent()) {

            LocalDateTime datetime = DateUtils.string2LocalDateTime((String) params.get("datetime"), "yyyy-MM-dd 00:00:00");
            onceStart = datetime;
            onceEnd = datetime.plusDays(1);
        }

        IPage<PhotoProblem> iPage = baseMapper
                .selectPage(page, new LambdaQueryWrapper<PhotoProblem>()
                        .ge(startDate != null, PhotoProblem::getCreateTime, startDate)
                        .lt(endDate != null, PhotoProblem::getCreateTime, endDate)
                        .ge(onceStart != null, PhotoProblem::getCreateTime, onceStart)
                        .lt(onceEnd != null, PhotoProblem::getCreateTime, onceEnd)
                        .like(Optional.ofNullable(params.get("createUserName")).isPresent(), PhotoProblem::getCreateUserName, params.get("createUserName"))
                        .eq(Optional.ofNullable(params.get("areaId")).isPresent(), PhotoProblem::getAreaId, params.get("areaId"))
                        .in(Optional.ofNullable(params.get("userIds")).isPresent(), PhotoProblem::getCreateUserId, (List<Integer>)params.get("userIds"))
                        .orderByDesc(PhotoProblem::getCreateTime));

        List<PhotoProblem> photoProblems = iPage.getRecords();

        photoProblems.forEach(e -> {
            CfgWorkArea cfgWorkArea = cfgWorkAreaService.getById(e.getAreaId());
            if (cfgWorkArea != null) {
                // 设置区域名称
                e.setAreaName(cfgWorkArea.getName());
            }
        });

        return new PageUtils(iPage);
    }

    @Override
    public void exportExcel(HttpServletResponse response, Map<String, Object> params) {

        ExportExcel exportExcel = new ExportExcel();

        params.put("limit", "65000");

        PageUtils pageUtils = queryPage(params);
        List list = pageUtils.getList();
        String fileName = "问题拍照记录";
        String sheetName = "sheet1";
        String[] headers = {"区域选择", "具体位置", "问题描述", "部门-记录人", "记录时间"};
        String[] columns = {"areaName", "position", "memo", "createUserName", "createTimeStr"};
        try {
            exportExcel.exportExcel(fileName, sheetName, headers, columns, list, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(RRExceptionEnum.EXPORT_FAIL);
        }
    }
}
